package com.omega_r.omegarecyclerview.viewpager;


import com.omega_r.libs.omegarecyclerview.viewpager.BannerViewHolder;
import com.omega_r.libs.omegarecyclerview.viewpager.OmegaPagerRecyclerView;
import com.omega_r.libs.omegarecyclerview.viewpager.OnBannerClickListener;
import com.omega_r.libs.omegarecyclerview.viewpager.Transformer;
import com.omega_r.omegarecyclerview.ResourceTable;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.PopupDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;


public class ViewPagerAbility extends Ability {
    private static String action = "";
    private MenuDialog dialog;
    private Text tv;
    private OmegaPagerRecyclerView viewPager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_view_pager);
        tv = (Text) findComponentById(ResourceTable.Id_style);
        findComponentById(ResourceTable.Id_dl).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showMenu();
            }
        });
        initData();
        viewPager = (OmegaPagerRecyclerView) findComponentById(ResourceTable.Id_banner);
        viewPager.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void onBannerClick(List datas, int position) {
                showToast("Clicked " + position);
            }
        });
        viewPager.setPages(getDataInt(), new CustomViewHolder()).setLoop(true).setAutoPlay(true)
                .start();
    }

    private void showMenu() {
        dialog = new MenuDialog(getContext(), tv);
        dialog.setDialogListener(() -> {
            dialog.destroy();
            return false;
        });
        dialog.show();
        dialog.setOnClickListener(() -> {
            dialog.hide();
            dialog.destroy();
            switch (action) {
                case "None":
                    viewPager.setBannerAnimation(transformers.get(0));
                    break;
                case "AccordionTransformer":
                    viewPager.setBannerAnimation(transformers.get(1));
                    break;
                case "BackgroundToForeground":
                    viewPager.setBannerAnimation(transformers.get(2));
                    break;
                case "CubeIn":
                    viewPager.setBannerAnimation(transformers.get(3));
                    break;
                case "CubeOut":
                    viewPager.setBannerAnimation(transformers.get(4));
                    break;
                case "DepthPage":
                    viewPager.setBannerAnimation(transformers.get(5));
                    break;
                case "Fade":
                    viewPager.setBannerAnimation(transformers.get(6));
                    break;
                case "Flip":
                    viewPager.setBannerAnimation(transformers.get(7));
                    break;
                case "ForegroundToBackground":
                    viewPager.setBannerAnimation(transformers.get(8));
                    break;
                case "RotateDown":
                    viewPager.setBannerAnimation(transformers.get(9));
                    break;
                case "RotateUp":
                    viewPager.setBannerAnimation(transformers.get(10));
                    break;
                case "Stack":
                    viewPager.setBannerAnimation(transformers.get(11));
                    break;
                case "Tablet":
                    viewPager.setBannerAnimation(transformers.get(12));
                    break;
                case "ZoomIn":
                    viewPager.setBannerAnimation(transformers.get(13));
                    break;
                case "ZoomOutSlide":
                    viewPager.setBannerAnimation(transformers.get(14));
                    break;

            }
            tv.setText(action);
        });

    }


    private enum DefaultTransformers {
        None("None"),
        Accordion("AccordionTransformer"),
        BackgroundToForeground("BackgroundToForeground"),
        CubeIn("CubeIn"),
        CubeOut("CubeOut"),
        DepthPage("DepthPage"),
        Fade("Fade"),
        Flip("Flip"),
        ForegroundToBackground("ForegroundToBackground"),
        RotateDown("RotateDown"),
        RotateUp("RotateUp"),
        Stack("Stack"),
        Tablet("Tablet"),
        ZoomIn("ZoomIn"),
        ZoomOutSlide("ZoomOutSlide");

        private String title;

        DefaultTransformers(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }
    }

    static class MenuDialog extends PopupDialog {
        private MenuDialog.onClickListener onClickListener;

        public MenuDialog(Context context, Component contentComponent) {
            super(context, contentComponent);
            init(context);
            int dialogWidth = AttrHelper.vp2px(240f, context);
            int dialogHeight = AttrHelper.vp2px(500f, context);
            setSize(dialogWidth, dialogHeight);
            setCornerRadius(10f);
            setOffset(AttrHelper.vp2px(5f, context), AttrHelper.vp2px(56f, context));
        }


        public void setOnClickListener(MenuDialog.onClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }

        private void init(Context context) {
            this.setTransparent(false);
            Menu inputView = new Menu(context);
            setCustomComponent(inputView);
            ListContainer listContainer1 = (ListContainer) inputView.findComponentById(ResourceTable.Id_list);
            MenuDialog.MenuProvider menuProvider = new MenuDialog.MenuProvider(DefaultTransformers.values(), context);
            listContainer1.setLongClickable(false);
            listContainer1.setItemProvider(menuProvider);
            listContainer1.setItemClickedListener((listContainer, component, i, l) -> {

                ViewPagerAbility.action = DefaultTransformers.values()[i].getTitle();
                onClickListener.onItemClick();
            });
        }

        public interface onClickListener {
            void onItemClick();
        }

        /**
         * Menu
         *
         * @author wz
         * @since 2021-06-28
         */
        static class Menu extends DirectionalLayout {
            Menu(Context context) {
                super(context);
                setupComponent(context);
            }

            private void setupComponent(Context context) {
                LayoutScatter.getInstance(context).parse(ResourceTable.Layout_pop, this, true);

            }
        }

        private static class MenuProvider extends BaseItemProvider {
            private final List<String> list;
            private final Context context;

            MenuProvider(DefaultTransformers[] array, Context context) {

                this.context = context;
                list = new ArrayList<>();
                for (int i = 0; i < array.length; i++) {
                    list.add(array[i].getTitle());
                }
            }

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                if (list != null && position >= 0 && position < list.size()) {
                    return list.get(position);
                }
                return new Object();
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
                final Component cpt;
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_pop, null, false);
                String str = list.get(position);
                Text text = (Text) cpt.findComponentById(ResourceTable.Id_text);
                text.setText(str);
                return cpt;
            }
        }
    }


    private ArrayList<Integer> getDataInt() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(ResourceTable.Media_image_1);
        list.add(ResourceTable.Media_image_3);
        list.add(ResourceTable.Media_image_1);
        list.add(ResourceTable.Media_image_2);
        return list;
    }

    private static class CustomViewHolder implements BannerViewHolder<Object> {

        @Override
        public Component createView(Context context, int position, Object data) {
            Component parse = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_image, null, false);
            Image img = (Image) parse.findComponentById(ResourceTable.Id_imageview);
            img.setPixelMap((Integer) data);
            return parse;
        }
    }


    private List<Class<? extends OmegaPagerRecyclerView.PageTransformer>> transformers = new ArrayList<>();

    public void initData() {
        transformers.add(Transformer.Default);
        transformers.add(Transformer.Accordion);
        transformers.add(Transformer.BackgroundToForeground);
        transformers.add(Transformer.CubeIn);
        transformers.add(Transformer.CubeOut);
        transformers.add(Transformer.DepthPage);
        transformers.add(Transformer.Fade);
        transformers.add(Transformer.FlipHorizontal);
        transformers.add(Transformer.ForegroundToBackground);
        transformers.add(Transformer.RotateDown);
        transformers.add(Transformer.RotateUp);
        transformers.add(Transformer.Stack);
        transformers.add(Transformer.Tablet);
        transformers.add(Transformer.ZoomIn);
        transformers.add(Transformer.ZoomOutSlide);
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    private void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(this)
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }
}

